﻿using Hidistro.Core;
using Hidistro.Entities.Commodities;
using Hidistro.SaleSystem.Catalog;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Hidistro.UI.SaleSystem.Tags
{
    public class wnxd_ShopCartHistory : WebControl
    {
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.RendHtml());
        }
        public string RendHtml()
        {
            StringBuilder builder = new StringBuilder();
            DataTable history = this.GetHistory();
            if (history != null && history.Rows.Count > 0)
            {
                builder.Append("<div id=\"history_box\" style=\"width:1000px;height:360px;margin:20px auto;\"><div style=\"font-size:16px;\">浏览过的商品：<span style=\"float:right;margin-right:40px;font-size:12px;\"><a id=\"clearBrowsedProduct\" href=\"javascript:void(0)\" style=\"color:#333;\">清除我的浏览记录</a></span></div><ul id=\"history\">");
                foreach (DataRow row in history.Rows)
                {
                    builder.Append("<li style=\"float:left;width:200px;margin:0 7px;padding:3px;\">");
                    string title = string.Empty;
                    string alt = string.Empty;
                    string keyword = row["Meta_Keywords"].ToString();
                    if (!string.IsNullOrEmpty(keyword))
                    {
                        string[] arr = keyword.Split('|');
                        if (arr.Length == 3)
                        {
                            title = arr[1];
                            alt = arr[2];
                        }
                    }
                    builder.AppendFormat("<a href=\"{0}\"><img src=\"{1}\" width=\"200px\" title=\"{2}\" alt=\"{3}\"></a>", Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }), Globals.GetImgUrl(row["ThumbnailUrl310"].ToString()), title, alt);
                    builder.AppendFormat("<p><a href=\"{1}\" alt=\"{0}\" style=\"color:#333;\">{0}</a>", row["ProductName"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                    if (row["ProductText"] != DBNull.Value) builder.AppendFormat("&nbsp;&nbsp;<a style=\"color:#F53864\" title=\"{0}\" href=\"{1}\">{0}</a>", row["ProductText"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                    if (row["SalePrice"] == DBNull.Value) row["SalePrice"] = row["MarketPrice"];
                    builder.AppendFormat("</p><p><b style=\"color:#55006F;\">￥{0}</b></p>", Globals.FormatMoney((decimal)row["SalePrice"]));
                    builder.Append("<div class=\"add_shop_cart\" style=\"height:24px;display:none;\">");
                    builder.AppendFormat("<a class=\"addToCart\" style=\"padding:3px 7px;background:url(.http://images.juseshop.com/images/sub_shopcatrbg.jpg) no-repeat;width:82px;height:17px;float:left;\" defaultsku=\"{0}\" stock=\"{1}\" href=\"javascript:void(0)\">", row["SkuId"], row["Stock"]);
                    builder.Append("<h4 style=\"margin-left:20px;\">加入购物车</h4></a></div></li>");
                }
                builder.Append("</ul></div>");
            }
            return builder.ToString();
        }
        private DataTable GetHistory()
        {
            IList<int> browedProductList = BrowsedProductQueue.GetBrowedProductList(this.MaxNum);
            if (browedProductList.Count == 0) return null;
            return ProductBrowser.GetVistiedProducts(browedProductList);
        }
        public int MaxNum { get; set; }
    }
}
